import { useRequest } from 'umi';
import { useMemo } from 'react';
import { getUserID, requestFunComponFiex } from '@/utils';
import { getBaseUrl } from '@/services/urlconst';
import { GetReceiveManageTab } from '../mconfig';
let baseUrl = getBaseUrl();
// let baseUrl = '/wenchuang'; // 本地环境

const ReceiveInfoServices = (curRes: Record<string, any>) => {
  /**获取权限参数 */
  const queryTypeObj: Record<string, any> = GetReceiveManageTab(curRes);

  /* 领用单列表接口 */
  const getReceiveInfoServices = () => {
    const request = useRequest(
      (requestParams?: Record<string, any>) => {
        const reqUrl = `${baseUrl}/assetApplyStuffasset/getApplyUesStuff/${queryTypeObj?.params}`;
        let params: any = {
          page: 1,
          rows: 10,
          ...requestParams,
          sort: 'createTime',
          order: 'desc',
          ...queryTypeObj,
          // createrID: getUserID(),
        };

        return requestFunComponFiex('POST', reqUrl, params);
      },
      {
        manual: true,
        paginated: true,
      },
    );

    const dataSource = useMemo(() => {
      let data = request.data?.obj;
      if (data?.length) {
        return data;
      } else {
        return [];
      }
    }, [request.data?.obj]);

    return {
      getReceiveListRun: (params: any) => request.run(params),
      getReceiveListDataSource: dataSource,
      getReceiveListLoading: request.loading,
      getReceiveListTableProps: (request as any).tableProps,
    };
  };

  /**查询领用单详情接口 */
  const findReceiveDetailServices = () => {
    const request = useRequest(
      (requestParams?: Record<string, any>) => {
        let reqUrl = `${baseUrl}/assetApplyStuffasset/getUesStuffDetail/${requestParams?.id}`;
        return requestFunComponFiex('POST', reqUrl);
      },
      {
        manual: true,
      },
    );

    return {
      findPlanDetailRun: request?.run,
      findPlanDetailLoading: request?.loading,
    };
  };

  return {
    findReceiveDetailServices,
    getReceiveInfoServices,
  };
};

export { ReceiveInfoServices };
